<script setup>

import {getCategoryStore} from "@/stores/getCategoryStore.js";

// import {onMounted, ref} from "vue";
import GoodsItem from "@/views/mainPage/home/components/GoodsItem.vue";
import {useItemList} from "@/composables/useItemList.js";
import {onMounted} from "vue";
import {watch} from "vue";

const categoryStore = getCategoryStore();
const {itemList, fetchItemList} = useItemList();
// 在组件挂载后执行


// 观察 categoryList 的变化
watch(
    () => categoryStore.categoryList,
    (newCategoryList) => {
      // 确保 categoryList 已被填充
      if (Object.keys(newCategoryList).length > 0) {
        Object.values(newCategoryList).forEach((brand) => {

          fetchItemList(brand.id);
        });
      }
    },
    { immediate: true } // 确保在观察开始时立即执行一次回调，如果该数据已经存在，直接执行回调
);

onMounted(() => {
  console.log("页面渲染完成");
  if (Object.keys(categoryStore.categoryList).length > 0) {
    Object.values(categoryStore.categoryList).forEach((brand) => {

      fetchItemList(brand.id);
    });
  }
});


</script>

<template>
  <div class="top-category">
    <div class="container m-top-20">
      <!-- 面包屑 -->
      <div class="bread-container">
        <el-breadcrumb separator=">">
          <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
          <el-breadcrumb-item>回收</el-breadcrumb-item>
        </el-breadcrumb>
      </div>
      <!-- 分类产品图片 -->
      <div class="sub-list">
        <h3>全部分类</h3>
        <ul>
          <li v-for="category in categoryStore.categoryList" :key="category.id" :title="category.name">
            <RouterLink :to="`product/subCategory/${category.id}`">
              <img v-ima-lazy="`./src/assets/images/${category.image}`" alt="" src=""/>
              <p>{{ category.name }}</p>
            </RouterLink>
          </li>
        </ul>
      </div>

      <!-- 分类产品列表 -->
      <div class="ref-goods" v-for="category in categoryStore.categoryList" :key="category.id">
        <div class="head">
          <h3>- {{ category.name }}-</h3>
        </div>
        <div class="body">
          <GoodsItem v-for="item in itemList.splice(0,10)" :item="item" :key="item.id"/>
        </div>
      </div>
    </div>


  </div>


</template>

<style scoped lang="scss">
.top-category {
  h3 {
    font-size: 28px;
    color: #666;
    font-weight: normal;
    text-align: center;
    line-height: 100px;
  }

  .sub-list {
    margin-top: 20px;
    background-color: #fff;

    ul {
      display: flex;
      padding: 0 32px;
      flex-wrap: wrap;

      li {
        width: 168px;
        height: 160px;


        a {
          text-align: center;
          display: block;
          font-size: 16px;

          img {
            width: 100px;
            height: 100px;
          }

          p {
            line-height: 40px;
          }

          &:hover {
            color: $xtxColor;
          }
        }
      }
    }
  }

  .ref-goods {
    background-color: #fff;
    margin-top: 20px;
    position: relative;

    .head {
      .xtx-more {
        position: absolute;
        top: 20px;
        right: 20px;
      }

      .tag {
        text-align: center;
        color: #999;
        font-size: 20px;
        position: relative;
        top: -20px;
      }
    }

    .body {
      display: flex;
      //justify-content: space-around;
      padding: 0 40px 30px;
      flex-wrap: wrap;

    }
  }

  .bread-container {
    padding: 25px 0;
  }
}
</style>